#include <stdio.h>
#include <string>
#include <vector>
#include <map>

using namespace std;

#include "bkbase.h"
#include "bstcp.h"

#include "h_base.h"
using namespace h_base;

#include "simbat_plugin.h"

#define TIME_OUT 10000

static bool FirstFlag = true;
static string Addr;
static int Port = 0;
static string Service;

static int HttpPost(const char *Header, const char *Body, int BodyLen, char ErrStr[200 + 1])
{
fprintf(stderr, "HEADER :\n%s\nBODY :\n%s\n", Header, Body);
return 0;
   int Sock = BsCreateClientSocket(Addr.c_str(), Port, TIME_OUT);

   if ( Sock < 0 )
   {
      snprintf(ErrStr, 200, "BsCreateClientSocket(%s, %d) fail, errno = %d", Addr.c_str(), Port, errno);
      return 8;
   }

   int r = BsWriteToSocket(Sock, Header, strlen(Header), TIME_OUT);

   if ( r < 0 )
   {
      snprintf(ErrStr, 200, "BsWriteToSocket fail, r = %d, errno = %d", r, errno);
      close(Sock);
      return 8;
   }

   r = BsWriteToSocket(Sock, Body, BodyLen, TIME_OUT);

   if ( r < 0 )
   {
      snprintf(ErrStr, 200, "BsWriteToSocket fail, r = %d, errno = %d", r, errno);
      close(Sock);
      return 8;
   }

   close(Sock);
   return 0;
}

int SimbatPluginPushAlarm(const char *InfoStr, char ErrStr[200 + 1])
{
   if ( FirstFlag )
   {
      char Url[80 + 1];
      void *Handle = NULL;
      ErrStr[0] = '\0';
      Handle = SimbatOpenConfig(ErrStr);
      if ( Handle == NULL )
      {
         return 8;
      }
      SimbatConfigGetValue(Handle, NULL, "URL_ALARM", Url, 80);
      const char *s = Url;
      int Pos = 0;
      if ( memcmp(s, "http://", 7) == 0 )
      {
         Pos += 7;
      }
      int Begin = Pos;
      for ( ; s[Pos] != '\0' && s[Pos] != ':' && s[Pos] != '/'; Pos++ )
         ;
      Addr = string(s + Begin, Pos - Begin);
      if ( s[Pos] == ':' )
      {
         Pos++;
         Port = BkStrToInt(s + Pos);
         for ( ; s[Pos] != '\0' && s[Pos] != '/'; Pos++ )
            ;
      }
      Service = string(s + Pos);
      if ( Addr == "" || Port <= 0 || Service == "" )
      {
         snprintf(ErrStr, 200, "Invalid url '%s'", Url);
         return 8;
      }
      SimbatCloseConfig(Handle);
      FirstFlag = false;
   }

   char Header[2048 + 1];
   char Body[2048 + 1];

   vector<HString> StrVec;

   SplitString(InfoStr, '|', StrVec, true);

   if ( StrVec.size() < 6 )
   {
      snprintf(ErrStr, 200, "Syntax error, '%s'", InfoStr);
      return 8;
   }

   const char *System = StrVec[0].c_str();
   const char *Time = StrVec[1].c_str();
   const char *Seq = StrVec[2].c_str();
   const char *BatchDate = StrVec[3].c_str();
   const char *Type = StrVec[4].c_str();
   const char *Msg = StrVec[5].c_str();

   snprintf(Body, 2048,
      "[{\"Severity\":\"2\","
      "\"Summary\":\"%s\","
      "\"LastOccurrence\":\"%.4s-%.2s-%.2s %.2s:%.2s:%.2s\","
      "\"stateChange\":\"\","
      "\"Status\":\"1\","
      "\"SourceID\":\"8\","
      "\"SourceIdentifier\":\"\","
      "\"SourceEventID\":\"%s\","
      "\"SourceCIName\":\"%s\","
      "\"SourceAlertKey\":\"%s\","
      "\"SourceSeverity\":\"\"}]",
      Msg, Time, Time + 4, Time + 4 + 2, Time + 4 + 2 + 2, Time + 4 + 2 + 2 + 2, Time + 4 + 2 + 2 + 2 + 2,
      Seq, System, Type);

   int BodyLen = strlen(Body);

   snprintf(Header, 2048,
      "POST %s HTTP/1.1\r\n"
      "Host: %s:%d\r\n"
      "Content-Type: application/json\r\n"
      "Content-Length: %d\r\n"
      "Connection: close\r\n"
      "\r\n",
      Service.c_str(), Addr.c_str(), Port, BodyLen);

   return HttpPost(Header, Body, BodyLen, ErrStr);
}
